<?php
        error_reporting(E_ERROR | E_WARNING | E_PARSE);
/*    $conn = mysql_connect("localhost","root","");
    $db = mysql_select_db("mydb",$conn);*/
   $conn = mysql_connect("localhost","s91199","123456");
    $db = mysql_select_db("s91199",$conn);
$query= $_POST['query'];
$from=$_POST['from'];
$to=$_POST['to'];
$report_title=$_POST['report_title'];
$sql="SELECT DISTINCT v.volunteer_firstname AS 'First name', v.volunteer_surname AS 'Surname', .v.volunteer_email AS 'Email', e.event_name AS 'Course name',  p.program_name AS 'Program name', sc.school_name AS 'School name'
FROM event e, volunteer v, trainer t, school sc, program p,session s
WHERE e.event_id = t.event_id
AND t.volunteer_id = v.volunteer_id
AND e.event_id = s.event_id
AND e.school_id = sc.school_id
AND p.program_id=e.program_id
AND t.trainer_status='confirmed'
AND s.session_date
BETWEEN '$from'
AND '$to'
ORDER BY v.volunteer_firstname";
    $rec = mysql_query($sql) or die (mysql_error());

    $num_fields = mysql_num_fields($rec);
    $title .= "$report_title"."\t";
    for($i = 0; $i < $num_fields; $i++ )
    {
        $header .= mysql_field_name($rec,$i)."\t";
    }

    while($row = mysql_fetch_row($rec))
    {
        $line = '';
        foreach($row as $value)
        {
            if((!isset($value)) || ($value == ""))
            {
                $value = "\t";
            }
            else
            {
                $value = str_replace( '"' , '""' , $value );
                $value = '"' . $value . '"' . "\t";
            }
            $line .= $value;
        }
        $data .= trim( $line ) . "\n";
    }

    $data = str_replace("\r" , "" , $data);

    if ($data == "")
    {
        $data = "\n No Record Found!\n";
    }

    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=reports.xls");
    header("Pragma: no-cache");
    header("Expires: 0");
    print "$title\n$header\n$data";

?>